package com.yic.module.base.service.center;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.base.dal.dataobject.item.ItemDO;
import com.yic.module.base.dal.redis.RedisKeyConstants;
import com.yic.module.base.service.centeruser.CenterUserService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.base.controller.admin.center.vo.*;
import com.yic.module.base.dal.dataobject.center.CenterDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.base.convert.center.CenterConvert;
import com.yic.module.base.dal.mysql.center.CenterMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.base.enums.ErrorCodeConstants.*;

/**
 * 工作中心 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class CenterServiceImpl implements CenterService {

    @Resource
    private CenterMapper centerMapper;
    @Resource
    private CenterUserService centerUserService;

    @Override
    public Long createCenter(CenterCreateReqVO createReqVO) {

        validateCreateOrUpdate(null, createReqVO.getName(), createReqVO.getCode());
        // 插入
        CenterDO center = CenterConvert.INSTANCE.convert(createReqVO);
        centerMapper.insert(center);
        // 返回
        return center.getId();
    }

    @Override
    @CacheEvict(cacheNames = RedisKeyConstants.CENTER, key = "#updateReqVO.id")
    public void updateCenter(CenterUpdateReqVO updateReqVO) {
        // 校验
        validateCreateOrUpdate(updateReqVO.getId(), updateReqVO.getName(), updateReqVO.getCode());
        // 更新
        CenterDO updateObj = CenterConvert.INSTANCE.convert(updateReqVO);
        centerMapper.updateById(updateObj);
    }

    @Override
    @CacheEvict(cacheNames = RedisKeyConstants.CENTER, key = "#id")
    @Transactional(rollbackFor = Exception.class)
    public void deleteCenter(Long id) {
        // 校验存在
        validateCenterExists(id);
        // 删除工作中心人员绑定
        centerUserService.deleteCenterUserByCenterId(id);
        // 删除
        centerMapper.deleteById(id);
    }

    private void validateCreateOrUpdate(Long id, String name, String code){
        validateNameExists(id, name);
        validateCodeExist(id, code);
    }

    private void validateNameExists(Long id, String name) {
        CenterDO center = centerMapper.selectByName(name);
        if (ObjectUtil.isEmpty(center)){
            return;
        }
        if (id == null){
            throw exception(CENTER_NAME_EXISTS);
        }
        if (!id.equals(center.getId())){
            throw exception(CENTER_NAME_EXISTS);
        }
    }

    private void validateCodeExist(Long id, String code){
        CenterDO center = centerMapper.selectByCode(code);
        if (ObjectUtil.isEmpty(center)){
            return;
        }
        if (id == null){
            throw exception(CENTER_CODE_EXISTS);
        }
        if (!id.equals(center.getId())){
            throw exception(CENTER_CODE_EXISTS);
        }
    }

    private void validateCenterExists(Long id) {
        if (centerMapper.selectById(id) == null) {
            throw exception(CENTER_NOT_EXISTS);
        }
    }

    @Override
    @Cacheable(cacheNames = RedisKeyConstants.CENTER, key = "#id")
    public CenterDO getCenter(Long id) {
        return centerMapper.selectById(id);
    }

    @Override
    public List<CenterDO> getCenterList(Collection<Long> ids) {
        return centerMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CenterDO> getCenterPage(CenterPageReqVO pageReqVO) {
        return centerMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CenterDO> getCenterList(CenterExportReqVO exportReqVO) {
        return centerMapper.selectList(exportReqVO);
    }

    @Override
    public List<CenterDO> getCenterList(Collection<Long> ids, Collection<Integer> statuses) {
        return centerMapper.selectList(ids, statuses);
    }

}
